﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="WeightedList.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2007, 2008, 2009, 2010 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Core.
//
//   starLiGHT.Core is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Core. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Core are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Collections
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;

    #endregion

    /// <summary>
    /// A list of values with a weight. If you select randomly values from the list
    /// the weight defines the chance of getting a value from the list.
    /// </summary>
    /// <typeparam name="T">
    /// The type of the values in the list.
    /// </typeparam>
    public class WeightedList<T> : FastList<WeightValuePair<T>>
    {
        private float totalWeights;
        private Random random = new Random();

        public float TotalWeight
        {
            get { return this.totalWeights; }
        }

        public T RandomValue
        {
            get
            {
                float position = this.random.NextFloat(0.0f, this.totalWeights);
                float current = 0.0f;
                int len = this.Count;
                for (int i = 0; i < len; ++i)
                {
                    current += base[i].Weight;
                    if (current >= position)
                    {
                        return base[i].Value;
                    }
                }

                return base[len - 1].Value;
            }
        }

        public new T this[int index]
        {
            get { return base[index].Value; }
            set { base[index] = new WeightValuePair<T>(base[index].Weight, value); }
        }

        public new void Clear()
        {
            base.Clear();
            this.totalWeights = 0.0f;
        }

        public void Add(T item)
        {
            this.totalWeights += 1.0f;
            base.Add(new WeightValuePair<T>(1.0f, item));
        }

        public void Add(float weight, T item)
        {
            this.totalWeights += weight;
            base.Add(new WeightValuePair<T>(weight, item));
        }

        public new void Add(WeightValuePair<T> wvp)
        {
            this.totalWeights += wvp.Weight;
            base.Add(wvp);
        }
    }
}
